'use strict';

stareal
    .controller("CurrencyExchangeController", function ($scope, $api,$state, $alert) {
        $scope.num = 0;
        $scope.max=6;
        $scope.showWalletTotal=0;
        $scope.subNum = function () {
            if ($scope.num <=0) {
                return;
            }
            $scope.num = $scope.num - 1;
        };
        $scope.addNum = function () {
            if ($scope.num == $scope.max) {
                //$alert.show("鹿仙贝不够啦!");
                return;
            }
            $scope.num = $scope.num + 1;
        };
        $("#myWalllet").height($(window).height());
    //兑换成功  my.exchangePaySuccess
        // 监听取票方式/地址/优惠券的变化,贝里，实时计算价格
        $scope.$watch('num', function (a, b) {
            $scope.showWallet= $scope.showWalletTotal;
            if($scope.num==''||$scope.num==undefined||$scope.num==null){
                $scope.numm=0;
            }
            if($scope.num<=$scope.max){
                $scope.showWallet= $scope.showWalletTotal-parseInt($scope.num*2);
            }else{
                $scope.num=$scope.max;
                $scope.showWallet= $scope.showWalletTotal-parseInt($scope.max*2);
            }

        }, true);
        $api.get("app/getShowcoin/getShowcoin",{}, true)
            .then(function(ret){
                // console.log(ret);
                $scope.showWallet=ret.showcoin;
                $scope.showWalletTotal=ret.showcoin;
                $scope.max=parseInt($scope.showWallet/2);
            });
          //应援棒兑换  app/getShowcoin/showcoinExchange
        $scope.showcoinExchange=function(){
              var _params={};
            _params.showcoin=$scope.showWalletTotal;
            _params.payType=1 ;//应援棒
            _params.payAmount=parseInt($scope.num*2);
            _params.payNum=$scope.num;
            if($scope.num==0){
                $alert.show('兑换应援棒数量不能为0!');
                return;
            }
            $api.post("app/getShowcoin/showcoinExchange", _params, true)
                .then(function (ret) {
                  if(ret.result){
                      $state.go("my.exchangePaySuccess",{showIn:ret.showcoin,getBar:$scope.num},true);
                  }
                },function(err){
                    $alert.show(err);
            })
        }

    });